<?php
/**
 * Ecart
 * 
 * This file is part of Ecart.
 * 
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @category    Ecart
 * @package     Ecart_Account
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 * 
 * @category    Ecart
 * @package     Ecart_Account
 * @subpackage  Controller
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Account_OrderController extends Ecart_Account_Controller_Account
{
    /**
     * @var Ecart_Sales_Model_Order
     */
    private $_table;
    
    public function init()
    {
        parent::init();
        $this->view->crumbs()->add(
            Ecart::translate('account')->__('My Orders'), '/account/order'
        );
        $this->_table =  Ecart::single('sales/order');
    }
    
    public function indexAction()
    {
        $this->view->pageTitle = Ecart::translate('account')->__(
            'My Orders'
        );
        $this->view->meta()->setTitle(
            Ecart::translate('account')->__(
                'My Orders'
        ));
        $this->view->orders = $this->_table->getOrdersByCustomer(
            Ecart::getCustomerId()
        );
        $this->render(); 
    }
    
    public function viewAction() 
    {
        $this->view->pageTitle = Ecart::translate('account')->__('View Order');
        if ($this->_hasParam('orderId')) {
            $orderId = $this->_getParam('orderId');
        } else { 
            $this->_redirect('/account/order');
        }
        
        $order = $this->_table->fetchAll(array (
            $this->db->quoteInto('customer_id = ?', Ecart::getCustomerId()),
            $this->db->quoteInto('site_id = ?', Ecart::getSiteId()),
            $this->db->quoteInto('id = ?', intval($orderId))
        ));
        
        if (!sizeof($order)) {
            $this->_redirect('/account/order');
        }
        $order = $order->current();
        
        $this->view->order = $order->toArray();
        $this->view->order['products'] = $order->getProducts();
        foreach($this->view->order['products'] as &$product) {
            // convert price with rates that was available
            // during order was created (not current rates)
            $product['price'] = Ecart::single('locale/currency')
                ->from(
                    $product['price'] * $order->currency_rate, 
                    $order->currency
                 );
            $product['final_price'] = Ecart::single('locale/currency')
                ->from(
                    $product['final_price'] * $order->currency_rate, 
                    $order->currency
                );
        }
        
        $this->view->order['totals'] = $order->getTotals();
        foreach ($this->view->order['totals'] as &$total) {
            // convert price with rates that was available
            // during order was created (not current rates)
            $total['value'] = Ecart::single('locale/currency')->from(
                $total['value'] * $order->currency_rate, $order->currency
            );
        }
        
        $this->view->order['billing'] = $order->getBilling();
        $this->view->order['delivery'] = $order->getDelivery();
        // convert price with rates that was available
        // during order was created (not current rates)
        $this->view->order['order_total'] = Ecart::single('locale/currency')
            ->from($order->order_total * $order->currency_rate, $order->currency);
        
        $this->render();
    }
    
    public function printAction()
    {
        if ($this->_hasParam('orderId')) {
            $orderId = $this->_getParam('orderId');
        } else { 
            $this->_redirect('/account/order');
        }
        $order = $this->_table->fetchAll(array (
            $this->db->quoteInto('customer_id = ?', Ecart::getCustomerId()),
            $this->db->quoteInto('site_id = ?', Ecart::getSiteId()),
            $this->db->quoteInto('id = ?', intval($orderId))
        ));
        
        if (!sizeof($order)) {
            $this->_redirect('/account/order');
        }
        $order = $order->current();
        
        $this->view->order = $order->toArray();
        $this->view->order['products']  = $order->getProducts();
        $this->view->order['totals']    = $order->getTotals();
        $this->view->order['billing']   = $order->getBilling();
        $this->view->order['delivery']  = $order->getDelivery();
        
        $this->_helper->layout->setLayout('default_print');
        $this->render();
    }
}